package fm.dongman.modellib.remote;

import android.support.annotation.NonNull;
import android.util.Log;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.model.ComicDataModel;
import fm.dongman.animefm.contractlib.model.ComicInfoDataModel;
import fm.dongman.animefm.contractlib.model.FArticleDataModel;
import fm.dongman.animefm.contractlib.model.FRankDataModel;
import fm.dongman.animefm.contractlib.model.FSheetDataModel;
import fm.dongman.animefm.contractlib.model.VideoSideTagModel;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.ArticleModel;
import fm.dongman.modellib.model.BannerModel;
import fm.dongman.modellib.model.ComicPictureModel;
import fm.dongman.modellib.model.ComicModel;
import fm.dongman.modellib.model.ComicPlayModel;
import fm.dongman.modellib.model.CommentModel;
import fm.dongman.modellib.model.MsgModel;
import fm.dongman.modellib.model.SheetModel;
import fm.dongman.modellib.model.SheetLabelModel;
import fm.dongman.modellib.model.UserModel;

/**
 * 网络解析操作类（单例模式）
 * Created by KUIGE on 2017/10/12.
 */

public class RemoteOperationHelper {

    //实例
    private static RemoteOperationHelper operationHelper;

    private RemoteOperationHelper() {
    }

    public static RemoteOperationHelper getOperationHelper() {
        if (operationHelper == null) {
            operationHelper = new RemoteOperationHelper();
        }
        return operationHelper;
    }

    /**
     * 首页 banner
     *
     * @param jsonObject 解析的目标
     * @return 数据包
     */
    List<IModel.IBannerModel> downBanner(JSONObject jsonObject) {
        JSONObject mObject;
        BannerModel bannerModel;
        List<IModel.IBannerModel> models = new ArrayList<>();
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("banner_list");
            for (int i = 0; i < array.length(); i++) {
                bannerModel = new BannerModel();
                JSONObject itemObj = array.getJSONObject(i);
                if (itemObj.has("img_url"))
                    bannerModel.setImgUrl(itemObj.optString("img_url"));
                if (itemObj.has("subject_id"))
                    bannerModel.setSubjectId(itemObj.optString("subject_id"));
                if (itemObj.has("title"))
                    bannerModel.setTitle(itemObj.optString("title"));
                models.add(bannerModel);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return models;
    }

    /**
     * 首页 独家推荐
     *
     * @param jsonObject 解析对象
     * @return 数据包
     */
    List<IModel.IComicModel> downFMRecommends(JSONObject jsonObject) {
        List<IModel.IComicModel> models = new ArrayList<>();
        JSONObject mObject;
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("e_recommend");
            for (int i = 0; i < array.length(); i++) {
                models.add(parseComicModel(array.getJSONObject(i)));
            }
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 首页 漫单推荐
     *
     * @param jsonObject 解析对象
     * @return 数据包
     */
    List<IModel.ISheetModel> downSheet(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        JSONObject mObject;
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("s_recommend");
            for (int i = 0; i < array.length(); i++) {
                models.add(parseSheetModel(array.getJSONObject(i)));
            }
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 首页 最新文章
     *
     * @param jsonObject 解析对象
     * @return 数据包
     */
    List<IModel.IArticleModel> downArticle(JSONObject jsonObject) {
        List<IModel.IArticleModel> models = new ArrayList<>();
        ArticleModel articleModel;
        JSONObject mObject;
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("newest_article");
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObj = array.getJSONObject(i);
                articleModel = parseArticleModel(itemObj);
                models.add(articleModel);
            }
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        return models;
    }


    /**
     * 首页 最新评论
     *
     * @param jsonObject 解析对象
     * @return 数据包
     */
    List<IModel.ICommentModel> downComment(JSONObject jsonObject) {
        List<IModel.ICommentModel> models = new ArrayList<>();
        JSONObject mObject;
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("newest_message");
            for (int i = 0; i < array.length(); i++) {
                JSONObject itemObj = array.getJSONObject(i);
                models.add(parseCommentModel(itemObj));
            }
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 解析评论模型
     */
    public CommentModel parseCommentModel(JSONObject itemObj) throws JSONException {
        CommentModel commentModel = new CommentModel();
        if (itemObj.has("create_time"))
            commentModel.setCreateTime(itemObj.optString("create_time"));
        if (itemObj.has("comment_time"))
            commentModel.setCreateTime(itemObj.optString("comment_time"));
        if (itemObj.has("content"))
            commentModel.setContent(itemObj.optString("content"));
        if (itemObj.has("time"))
            commentModel.setTime(itemObj.optString("time"));
        if (itemObj.has("user"))
            commentModel.setUser(parseUserModel(itemObj.getJSONObject("user")));
        if (itemObj.has("subject_id"))
            commentModel.setSubjectId(itemObj.optString("subject_id"));
        if (itemObj.has("title"))
            commentModel.setTitle(itemObj.optString("title"));
        return commentModel;
    }

    /**
     * 首页 人气番剧
     *
     * @param jsonObject 解析对象
     * @return 数据包
     */
    List<IModel.IComicModel> downRankComic(JSONObject jsonObject) {
        List<IModel.IComicModel> models = new ArrayList<>();
        JSONObject mObject;
        try {
            mObject = jsonObject.getJSONObject("data");
            JSONArray array = mObject.getJSONArray("hot_bangumi");
            for (int i = 0; i < array.length(); i++) {
                models.add(parseComicModel(array.getJSONObject(i)));
            }
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 用户登陆
     */
    UserModel downUser(JSONObject jsonObject) {
        UserModel model = new UserModel();
        try {
            JSONObject object = jsonObject.getJSONObject("data");
            model = parseUserModel(object);
        } catch (Exception e) {
            Log.e("错误", e.getMessage() + e.getCause());
            e.printStackTrace();
        }
        if (model.getToken() != null) {
            StaticUrl.TOKEN_KEY = model.getToken();
            StaticUrl.RETOKEN_KEY = model.getRefreshToken();
            Log.e("----", model.getToken() + "      " + model.getRefreshToken());
        }
        return model;
    }

    /**
     * 漫圈
     */
    FArticleDataModel downFArticle(JSONObject jsonObject) {
        FArticleDataModel FartclList = new FArticleDataModel();
        List<IModel.IArticleModel> newArticles = new ArrayList<>();
        List<IModel.IArticleModel> bannerlist = new ArrayList<>();
        List<IModel.IArticleModel> hotList = new ArrayList<>();
        try {
            JSONObject DataObg = jsonObject.getJSONObject("data");
            JSONArray arrHot = DataObg.getJSONArray("hot");
            JSONArray arrArrList = DataObg.getJSONArray("list");
            JSONArray arrBanner = DataObg.getJSONArray("banner");

            //最新文章
            for (int i = 0; i < arrArrList.length(); i++) {
                JSONObject jsonObj = arrArrList.getJSONObject(i);
                newArticles.add(parseArticleModel(jsonObj));
            }

            //banner列表
            ArticleModel banner;
            for (int i = 0; i < arrBanner.length(); i++) {
                banner = new ArticleModel();
                JSONObject jsonObg = arrBanner.getJSONObject(i);
                if (jsonObg.has("article_id"))
                    banner.setArticleId(jsonObg.optString("article_id"));
                if (jsonObg.has("title"))
                    banner.setTitle(jsonObg.optString("title"));
                if (jsonObg.has("img_url"))
                    banner.setImgUrl(jsonObg.optString("img_url"));
                bannerlist.add(banner);
            }
            //本周最火
            for (int i = 0; i < arrHot.length(); i++) {
                JSONObject jsonObj = arrHot.getJSONObject(i);
                hotList.add(parseArticleModel(jsonObj));
            }
        } catch (JSONException e) {
            Log.e(getClass().getName(), e.getMessage() + "：漫圈解析出现问题");
            e.printStackTrace();
        }

        FartclList.setHotArticles(hotList);
        FartclList.setDynamicArticles(bannerlist);
        FartclList.setNewArticles(newArticles);
        return FartclList;
    }


    /**
     * 解析漫单json（发现-->漫单）
     */
    FSheetDataModel downMandan(JSONObject jsonObject) {
        FSheetDataModel model = new FSheetDataModel();
        List<IModel.ISheetLabelModel> label = new ArrayList<>();//标签
        List<IModel.ISheetModel> sheetModels = new ArrayList<>();//漫单
        List<IModel.IUserModel> userModels = new ArrayList<>();//漫单达人
        List<IModel.ISheetModel> topicModels = new ArrayList<>();//专题列表
        SheetModel topicSheet;

        try {
            JSONObject object = jsonObject.getJSONObject("data");
            JSONObject mandan = object.getJSONObject("mandan");
            JSONObject topic = object.getJSONObject("topic");
            JSONObject json;

            //专题列表
            JSONArray list = topic.getJSONArray("list");
            for (int i = 0; i < list.length(); i++) {
                json = list.getJSONObject(i);
                topicModels.add(parseSheetModel(json));
            }

            //漫单分类
            JSONArray tag = topic.getJSONArray("tag");
            SheetLabelModel tagmodel;
            for (int i = 0; i < tag.length(); i++) {
                json = tag.getJSONObject(i);
                tagmodel = new SheetLabelModel();
                if (json.has("tag_id"))
                    tagmodel.setTagId(json.optString("tag_id"));
                if (json.has("tag_img"))
                    tagmodel.setTagImg(json.optString("tag_img"));
                if (json.has("tag_name"))
                    tagmodel.setTagName(json.optString("tag_name"));
                label.add(tagmodel);
            }

            //漫单列表
            list = mandan.getJSONArray("list");
            for (int i = 0; i < list.length(); i++) {
                json = list.getJSONObject(i);
                topicSheet = parseSheetModel(json);
                if (json.has("user"))
                    topicSheet.setUser(parseUserModel(json.getJSONObject("user")));
                sheetModels.add(topicSheet);
            }

            //漫单达人
            list = mandan.getJSONArray("daren");
            for (int i = 0; i < list.length(); i++) {
                json = list.getJSONObject(i);
                userModels.add(parseUserModel(json));
            }

        } catch (Exception e) {
            Log.e(e.getClass().getName(), e.getMessage());
            e.printStackTrace();
        }
        model.setMLabels(label);
        model.setMasters(userModels);
        model.setSpecials(topicModels);
        model.setSheets(sheetModels);
        return model;
    }

    /**
     * 解析番剧页面
     */
    ComicDataModel downComicList(JSONObject jsonObject) {
        ComicDataModel model = new ComicDataModel();
        List<IModel.IComicModel> newComics = new ArrayList<>();   //最新速递
        List<IModel.IComicModel> recommendComics = new ArrayList<>();//推荐番剧
        List<IModel.IComicModel> guessLikeComics = new ArrayList<>();//猜你喜欢
        List<IModel.ICommentModel> newComment = new ArrayList<>();  //最新槽点
        String quarter = "0"; // 当前季度
        JSONObject object;
        JSONArray jsonArray;
        try {
            // 最新速递
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("recent");
            for (int i = 0; i < jsonArray.length(); i++) {
                newComics.add(parseComicModel(jsonArray.getJSONObject(i)));
            }

            // 推荐番剧
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("xinfan");
            for (int i = 0; i < (jsonArray.length() > 12 ? 12 : jsonArray.length()); i++) {
                recommendComics.add(parseComicModel(jsonArray.getJSONObject(i)));
            }

            // 吐槽
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("tucao");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                newComment.add(parseCommentModel(object));
            }

            // 猜你喜欢
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("guess_like");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                guessLikeComics.add(parseComicModel(object));
            }

            // 获取当前季度
            quarter = jsonObject.getJSONObject("data").getString("curr_season");
            quarter = obtainQuarter(quarter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.setQuarter(quarter);
        model.setNewComics(newComics);
        model.setNewComment(newComment);
        model.setRecommendComics(recommendComics);
        model.setGuessLikeComics(guessLikeComics);
        return model;
    }

    @NonNull
    private String obtainQuarter(String quarter) {
        if (quarter.substring(quarter.length() - 2, quarter.length()).equals("0")) {
            quarter = quarter.substring(quarter.length() - 1, quarter.length());
        } else {
            quarter = quarter.substring(quarter.length() - 2, quarter.length());
        }
        return quarter;
    }

    /**
     * 榜单页面数据解析
     */
    FRankDataModel downFrankData(JSONObject json) {
        FRankDataModel FRank = new FRankDataModel();
        List<IModel.IComicModel> weekHotComicRank = new ArrayList<>(); //本周最火
        List<IModel.IComicModel> comicTopRank = new ArrayList<>(); //新番top
        List<IModel.IComicModel> domesticComicRank = new ArrayList<>(); //国产排行
        List<IModel.IComicModel> haremComicRank = new ArrayList<>(); //后宫排行
        ComicModel comic;
        try {
            JSONObject DataObg = json.getJSONObject("data");
            JSONArray arrHot = DataObg.getJSONObject("week_hot").getJSONArray("list");
            JSONArray arrNew = DataObg.getJSONObject("xinfan").getJSONArray("list");
            JSONArray arrGc = DataObg.getJSONObject("guochan").getJSONArray("list");
            JSONArray arrHg = DataObg.getJSONObject("hougong").getJSONArray("list");
            for (int i = 0; i < arrHot.length(); i++) {
                comic = new ComicModel();
                // 本周最火
                JSONObject hotObject = arrHot.getJSONObject(i);
                comic.setId(hotObject.optString("subject_id"));
                comic.setTitle(hotObject.optString("subject_title"));
                weekHotComicRank.add(comic);

                // 新番动漫
                JSONObject newObject = arrNew.getJSONObject(i);
                comic.setId(newObject.optString("subject_id"));
                comic.setTitle(newObject.optString("subject_title"));
                comicTopRank.add(comic);

                // 国产动漫
                JSONObject gcObject = arrGc.getJSONObject(i);
                comic.setId(gcObject.optString("subject_id"));
                comic.setTitle(gcObject.optString("subject_title"));
                domesticComicRank.add(comic);

                // 后宫动漫
                JSONObject hgObject = arrHg.getJSONObject(i);
                comic.setId(hgObject.optString("subject_id"));
                comic.setTitle(hgObject.optString("subject_title"));
                haremComicRank.add(comic);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        FRank.setHaremComicRank(weekHotComicRank);
        FRank.setComicTopRank(comicTopRank);
        FRank.setDomesticComicRank(domesticComicRank);
        FRank.setWeekHotComicRank(haremComicRank);
        return FRank;
    }

    /**
     * 解析搜索关键字
     */
    List<String> downSearchHotWords(JSONObject object) {
        List<String> mList = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                mList.add(jsonArray.getString(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mList;
    }

    /**
     * 解析榜单详情列表
     */
    List<IModel.IComicModel> downRankDetailsList(JSONObject object) {
        List<IModel.IComicModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseComicModel(jsonObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }


    /**
     * 解析漫圈文章地址信息
     */
    public IModel.IArticleModel downArticleDetails(JSONObject object) {
        ArticleModel model = new ArticleModel();
        try {
            JSONObject jsonObject = object.getJSONObject("data");
            model = parseArticleModel(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 解析漫单详情信息
     */
    public IModel.ISheetModel downSheetDetails(JSONObject object) {
        List<IModel.IComicModel> models = new ArrayList<>();
        SheetModel model = new SheetModel();
        try {
            JSONObject dataObject = object.getJSONObject("data");
            JSONObject jsonObject;
            JSONArray jsonArray;

            if (dataObject.has("info")) {
                jsonObject = dataObject.getJSONObject("info");
                jsonArray = dataObject.getJSONArray("list");
            } else {
                jsonObject = dataObject.getJSONObject("list").getJSONObject("info");
                jsonArray = dataObject.getJSONObject("list").getJSONArray("list");
            }
            model = parseSheetModel(jsonObject);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                models.add(parseComicModel(obj));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        model.setmMandaList(models);
        return model;
    }

    /**
     * （Alex）解析漫单信息
     */
    private SheetModel parseSheetModel(JSONObject jsonObject) throws JSONException {
        SheetModel model = new SheetModel();

        if (jsonObject.has("id"))
            model.setMandanId(jsonObject.optString("id"));
        if (jsonObject.has("topic_id")) //动漫相关漫单
            model.setMandanId(jsonObject.optString("topic_id"));

        if (jsonObject.has("title"))
            model.setMandanTitle(jsonObject.optString("title"));
        if (jsonObject.has("topic_title"))
            model.setMandanTitle(jsonObject.optString("topic_title"));

        if (jsonObject.has("summary"))
            model.setSummary(jsonObject.optString("summary"));
        if (jsonObject.has("create_time"))
            model.setCreateTime(jsonObject.optString("create_time"));
        if (jsonObject.has("browse_count"))
            model.setBrowseCount(jsonObject.optString("browse_count"));
        if (jsonObject.has("type"))
            model.setType(jsonObject.optString("type"));

        if (jsonObject.has("img_url"))
            model.setMandanImg(jsonObject.optString("img_url"));
        if (jsonObject.has("topic_img"))
            model.setMandanImg(jsonObject.optString("topic_img"));

        if (jsonObject.has("subject_count"))
            model.setAnimeCount(jsonObject.getInt("subject_count"));
        if (jsonObject.has("bangumi_count"))
            model.setAnimeCount(jsonObject.getInt("bangumi_count"));

        if (jsonObject.has("collect_count"))
            model.setCollectCount(jsonObject.optString("collect_count"));
        if (jsonObject.has("is_collect"))
            model.setmIsCollected(jsonObject.getInt("is_collect"));
        if (jsonObject.has("user"))
            model.setUser(parseUserModel(jsonObject.getJSONObject("user")));

        return model;
    }

    /**
     * （Alex）解析用户信息
     */
    private UserModel parseUserModel(JSONObject userJsonObj) throws JSONException {
        UserModel userModel = new UserModel();

        if (userJsonObj.has("id"))
            userModel.setUserId(userJsonObj.optString("id"));
        if (userJsonObj.has("user_id")) //登陆 漫单达人 粉丝和关注
            userModel.setUserId(userJsonObj.optString("user_id"));

        if (userJsonObj.has("name"))
            userModel.setUserName(userJsonObj.optString("name"));
        if (userJsonObj.has("user_name"))   //漫单达人 粉丝和关注
            userModel.setUserName(userJsonObj.optString("user_name"));

        if (userJsonObj.has("avatar_url"))
            userModel.setAvatarUrl(userJsonObj.optString("avatar_url"));
        if (userJsonObj.has("gender"))
            userModel.setGender(userJsonObj.optString("gender"));
        if (userJsonObj.has("summary"))
            userModel.setDescription(userJsonObj.optString("summary"));
        if (userJsonObj.has("token"))
            userModel.setToken(userJsonObj.optString("token"));
        if (userJsonObj.has("refresh_token"))
            userModel.setRefreshToken(userJsonObj.optString("refresh_token"));
        if (userJsonObj.has("remind_count"))
            userModel.setMessageNum(userJsonObj.optString("remind_count"));
        if (userJsonObj.has("fan_count"))
            userModel.setFansNum(userJsonObj.optString("fan_count"));
        if (userJsonObj.has("follow_count"))
            userModel.setFlowNum(userJsonObj.optString("follow_count"));
        if (userJsonObj.has("description"))
            userModel.setDescription(userJsonObj.optString("description"));
        if (userJsonObj.has("phone"))
            userModel.setPhone(userJsonObj.optString("phone"));
        if (userJsonObj.has("mandan_count"))
            userModel.setSheetCount(userJsonObj.optString("mandan_count"));
        if (userJsonObj.has("comment_count"))
            userModel.setCommentCount(userJsonObj.optString("comment_count"));
        if (userJsonObj.has("chat_count"))
            userModel.setMessageNum(userJsonObj.optString("chat_count"));

        return userModel;
    }

    /**
     * （Alex）解析文章信息
     */
    private ArticleModel parseArticleModel(JSONObject itemObj) throws JSONException {
        ArticleModel articleModel = new ArticleModel();
        if (itemObj.has("id"))
            articleModel.setArticleId(itemObj.optString("id"));
        if (itemObj.has("article_id"))
            articleModel.setArticleId(itemObj.optString("article_id"));

        if (itemObj.has("title"))
            articleModel.setTitle(itemObj.optString("title"));
        if (itemObj.has("article_url"))
            articleModel.setArticleURL(itemObj.optString("article_url"));

        if (itemObj.has("img_url"))
            articleModel.setImgUrl(itemObj.optString("img_url"));
        if (itemObj.has("thumb_img_url"))   //漫圈最火
            articleModel.setImgUrl(itemObj.optString("thumb_img_url"));

        if (itemObj.has("browse_count"))
            articleModel.setBrowseCount(itemObj.optString("browse_count"));
        if (itemObj.has("comment_count"))
            articleModel.setCommentCount(itemObj.optString("comment_count"));
        if (itemObj.has("create_time"))
            articleModel.setCreateTime(itemObj.optString("create_time"));
        if (itemObj.has("summary"))
            articleModel.setSummary(itemObj.optString("summary"));
        if (itemObj.has("content") && !itemObj.isNull("content")) //cosplay
            articleModel.setSummary(itemObj.optString("content"));
        if (itemObj.has("type"))
            articleModel.setType(itemObj.optString("type"));
        if (itemObj.has("source_url"))
            articleModel.setSourceURL(itemObj.optString("source_url"));
        if (itemObj.has("thumbs"))
            articleModel.setThumbs(itemObj.optString("thumbs").split("##"));
        return articleModel;
    }

    /**
     * 解析动漫模型 Json
     *
     * @param obj json
     * @return 动漫模型ComicModel
     */
    public ComicModel parseComicModel(JSONObject obj) throws JSONException {
        ComicModel comicModel = new ComicModel();
        if (obj.has("id"))
            comicModel.setId(obj.optString("id"));
        if (obj.has("subject_id")) // 看过的动漫
            comicModel.setId(obj.optString("subject_id"));

        if (obj.has("title"))
            comicModel.setTitle(obj.optString("title"));
        if (obj.has("subject_title"))
            comicModel.setTitle(obj.optString("subject_title"));

        if (obj.has("aka"))
            comicModel.setAka(obj.optString("aka"));

        if (obj.has("year"))
            comicModel.setYear(obj.optString("year"));

        if (obj.has("browse_count"))
            comicModel.setBrowseCount(obj.optString("browse_count"));

        if (obj.has("collect_count"))
            comicModel.setCollectCount(obj.optString("collect_count"));

        if (obj.has("casts"))
            comicModel.setCasts(obj.optString("casts"));

        if (obj.has("director"))
            comicModel.setDirector(obj.optString("director"));
        if (obj.has("directors"))
            comicModel.setDirector(obj.optString("directors"));

        if (obj.has("insert_time"))
            comicModel.setInsertTime(obj.optString("insert_time"));
        if (obj.has("episode_update_time"))
            comicModel.setInsertTime(obj.optString("episode_update_time"));
        if (obj.has("update_time")) // 时间表
            comicModel.setInsertTime(obj.optString("update_time"));

        if (obj.has("watch_time"))
            comicModel.setWatchTime(obj.optString("watch_time"));

        if (obj.has("recom_intro"))
            comicModel.setRecomeIntro(obj.optString("recom_intro"));

        if (obj.has("img_url"))
            comicModel.setImgUrl(obj.optString("img_url"));
        if (obj.has("img_large"))
            comicModel.setImgUrl(obj.optString("img_large"));

        if (obj.has("country"))
            comicModel.setCountry(obj.optString("country"));
        if (obj.has("countries"))
            comicModel.setCountry(obj.optString("countries"));

        if (obj.has("summary"))
            comicModel.setSummary(obj.optString("summary"));

        if (obj.has("genre"))
            comicModel.setGenre(obj.optString("genre"));
        if (obj.has("genres")) //动漫详情
            comicModel.setGenre(obj.optString("genres"));


        if (obj.has("is_end"))
            comicModel.setIsEnd(obj.optString("is_end"));

        if (obj.has("episodes"))
            comicModel.setEpisodes(obj.optString("episodes"));

        if (obj.has("subtype"))
            comicModel.setSubType(obj.optString("subtype"));

        if (obj.has("state"))
            comicModel.setIsEnd(obj.optString("state"));

        if (obj.has("rate_score")) //首页独家推荐
            comicModel.setRateScore(obj.optString("rate_score"));
        if (obj.has("total_score")) //动漫详情
            comicModel.setRateScore(obj.optString("total_score"));
        if (obj.has("rating"))  //搜索结果
            comicModel.setRateScore(obj.optString("rating"));
        if (obj.has("rate_average"))    //榜单
            comicModel.setRateScore(obj.optString("rate_average"));


        if (obj.has("score_story_avg"))
            comicModel.setStoryScore(obj.optString("score_story_avg"));
        if (obj.has("score_picture_avg"))
            comicModel.setPictureScore(obj.optString("score_picture_avg"));
        if (obj.has("score_music_avg"))
            comicModel.setMusicScore(obj.optString("score_music_avg"));
        if (obj.has("score_character_avg"))
            comicModel.setCharacterScore(obj.optString("score_character_avg"));

        return comicModel;
    }

    /**
     * 解析关注的用户集合
     */
    public List<IModel.IUserModel> downFollow(JSONObject object) {
        List<IModel.IUserModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONObject("data").getJSONArray("follows");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseUserModel(jsonObject));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 解析关注的用户集合
     */
    public List<IModel.IUserModel> downFans(JSONObject object) {
        List<IModel.IUserModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONObject("data").getJSONArray("fans");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseUserModel(jsonObject));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return models;
    }

    /**
     * 获取专题列表
     */
    public List<IModel.ISheetModel> downTopicListData(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取漫单列表
     */
    public List<IModel.ISheetModel> downSheetListData(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取动漫评论列表
     */
    public List<IModel.ICommentModel> downCommentComicListData(JSONObject object) {
        List<IModel.ICommentModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseCommentModel(jsonObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取文章评论列表
     */
    public List<IModel.ICommentModel> downCommentArticleData(JSONObject object) {
        List<IModel.ICommentModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseCommentModel(jsonObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取漫单评论列表
     */
    public List<IModel.ICommentModel> downCommentSheetData(JSONObject object) {
        List<IModel.ICommentModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = object.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                models.add(parseCommentModel(jsonObject));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取用户详细数据
     */
    public IModel.IUserModel downUserInfo(JSONObject jsonObject) {
        UserModel model = new UserModel();
        try {
            JSONObject objectData = jsonObject.getJSONObject("data");
            model = parseUserModel(objectData.optJSONObject("user_data"));
            if (objectData.has("is_follow"))
                model.setmIsFollow(objectData.optString("is_follow"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 获取用户的评论列表
     */
    public List<? extends IModel.ICommentModel> downUser_CommentList(JSONObject jsonObject) {
        List<IModel.ICommentModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseCommentModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取用户关注的列表
     */
    public List<IModel.ISheetModel> downUserFollowSheets(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取用户收藏的专题列表
     */
    public List<IModel.ISheetModel> downUserCollectionTopics(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取用户创建的漫单
     */
    public List<IModel.ISheetModel> downUserCreateSheets(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取发现-》漫圈-》分类数据->cosplay
     */
    public List<IModel.IArticleModel> downFArtcileList(JSONObject jsonObject) {
        List<IModel.IArticleModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseArticleModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 动漫详细信息
     */
    public ComicInfoDataModel downComicInfo(JSONObject jsonObject) {

        ComicInfoDataModel dataModel = new ComicInfoDataModel();
        ComicModel model;
        JSONObject object;
        JSONArray jsonArray;
        try {

            object = jsonObject.getJSONObject("data").getJSONObject("subject");
            model = parseComicModel(object);

            model.setPlaylist(getPlay(jsonObject));

            //动漫剧照
            model.setImgList(getComicPicturesList(jsonObject));

            dataModel.setComicModel(model);

            JSONObject dataObj = jsonObject.getJSONObject("data");

            //相关番剧
            List<IModel.IComicModel> comicModels = new ArrayList<>();
            if (isJsonContainsPropertyData(dataObj, "relative_subjects")) {
                jsonArray = dataObj.getJSONArray("relative_subjects");
                for (int i = 0; i < jsonArray.length(); i++) {
                    object = jsonArray.getJSONObject(i);
                    comicModels.add(parseComicModel(object));
                }
            }
            dataModel.setRelatedComics(comicModels);

            //相关文章
            List<IModel.IArticleModel> articleModels = new ArrayList<>();
            if (isJsonContainsPropertyData(dataObj, "relative_articles")) {
                jsonArray = dataObj.getJSONArray("relative_articles");
                for (int i = 0; i < jsonArray.length(); i++) {
                    object = jsonArray.getJSONObject(i);
                    articleModels.add(parseArticleModel(object));
                }
            }
            dataModel.setRelatedArticles(articleModels);

            //相关漫单
            List<IModel.ISheetModel> sheetModels = new ArrayList<>();
            if (isJsonContainsPropertyData(dataObj, "relative_topics")) {
                jsonArray = dataObj.getJSONArray("relative_topics");
                for (int i = 0; i < jsonArray.length(); i++) {
                    if (jsonArray.isNull(i))
                        continue;
                    object = jsonArray.getJSONObject(i);
                    sheetModels.add(parseSheetModel(object));
                }
            }
            dataModel.setRelatedSheets(sheetModels);

            // 相关评论（热门评论）
            List<IModel.ICommentModel> commentModels = new ArrayList<>();
            if (isJsonContainsPropertyData(dataObj, "comments")) {
                jsonArray = dataObj.getJSONArray("comments");
                for (int i = 0; i < jsonArray.length(); i++) {
                    if (jsonArray.isNull(i))
                        continue;
                    object = jsonArray.getJSONObject(i);
                    commentModels.add(parseCommentModel(object));
                }
            }
            dataModel.setRelatedComment(commentModels);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataModel;
    }


    //检测json中是否有属性并且值不能为null
    private boolean isJsonContainsPropertyData(JSONObject jsonObject, String propertyName) {
        return jsonObject.has(propertyName) && (!jsonObject.isNull(propertyName));
    }

    /**
     * 获取播放列表
     */
    private List<ComicPlayModel> getPlay(JSONObject jsonObject) {

        //播放列表
        List<ComicPlayModel> comicPlayModels = new ArrayList<>();
        ComicPlayModel comicPlayModel;
        JSONArray jsonArray;
        JSONObject object;
        try {
            JSONObject dataObj = jsonObject.getJSONObject("data");
            if (!dataObj.has("playlist") || dataObj.isNull("playlist"))
                return comicPlayModels;
            jsonArray = dataObj.getJSONArray("playlist");
            //动漫列表
            for (int n = 0; n < jsonArray.length(); n++) {
                object = jsonArray.getJSONObject(n);
                comicPlayModel = new ComicPlayModel();
                if (object.has("play_url"))
                    comicPlayModel.setDomain(object.optString("play_url"));
                if (object.has("site"))
                    comicPlayModel.setSource(object.optString("site"));
                if (object.has("id"))
                    comicPlayModel.setId(object.optString("id"));
                if (object.has("episodes"))
                    comicPlayModel.setNum(object.optString("episodes"));
                comicPlayModels.add(comicPlayModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comicPlayModels;
    }

    /**
     * 动漫剧照
     */
    private List<ComicPictureModel> getComicPicturesList(JSONObject jsonObject) throws JSONException {
        List<ComicPictureModel> models = new ArrayList<>();
        JSONArray jsonPicArray;
        JSONObject dataObject = jsonObject.getJSONObject("data");

        if (!dataObject.has("pictures") || dataObject.isNull("pictures"))
            return models;
        jsonPicArray = dataObject.getJSONArray("pictures");

        for (int i = 0; i < jsonPicArray.length(); i++) {
            ComicPictureModel model = new ComicPictureModel();
            JSONObject object = jsonPicArray.getJSONObject(i);
            if (object.has("large_url"))
                model.setMaxImg(object.optString("large_url"));
            if (object.has("middle_url"))
                model.setImg(object.optString("middle_url"));
            if (object.has("picture_id"))
                model.setImgId(object.optString("picture_id"));
            if (object.has("small_url"))
                model.setMinImg(object.optString("small_url"));
            models.add(model);
        }
        return models;
    }

    /**
     * 搜索的用户数据集
     */
    public List<IModel.IUserModel> downSearchUser(JSONObject jsonObject) {
        List<IModel.IUserModel> models = new ArrayList<>();
        JSONArray jsonArray;
        JSONObject object;
        try {
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                models.add(parseUserModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取文章列表
     */
    public List<IModel.IArticleModel> downArticleList(JSONObject jsonObject) {
        List<IModel.IArticleModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseArticleModel(object));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 搜索的漫单数据
     */
    public List<IModel.ISheetModel> downSearchSheet(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        JSONArray jsonArray;
        JSONObject object;
        try {
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 私信列表
     */
    public List<IModel.IMsgModel> downCharList(JSONObject jsonObject) {
        List<IModel.IMsgModel> models = new ArrayList<>();
        MsgModel model;
        JSONArray jsonArray;
        JSONObject object;
        try {
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                model = new MsgModel();
                if (object.has("chart_id"))
                    model.setChartId(object.optString("chart_id"));
                if (object.has("content"))
                    model.setContent(object.optString("content"));
                if (object.has("create_time"))
                    model.setCreateTime(object.optString("create_time"));
                if (object.has("id"))
                    model.setId(object.optString("id"));
                if (object.has("cnt"))
                    model.setNum(object.optString("cnt"));
                if (object.has("target_name"))
                    model.setTagName(object.optString("target_name"));
                if (object.has("target_url"))
                    model.setTagUrl(object.optString("target_url"));
                if (object.has("target_user_id"))
                    model.setUserId(object.optString("target_user_id"));
                if (object.has("target_user_avatar_url"))
                    model.setUserUrl(object.optString("target_user_avatar_url"));
                models.add(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }


    /**
     * 私信数据
     */
    public List<IModel.IMsgModel> downCharModel(JSONObject jsonObject) {
        List<IModel.IMsgModel> models = new ArrayList<>();
        MsgModel model;
        JSONArray jsonArray;
        JSONObject object;
        try {
            jsonArray = jsonObject.getJSONObject("data").getJSONArray("charts");
            for (int i = 0; i < jsonArray.length(); i++) {
                object = jsonArray.getJSONObject(i);
                model = new MsgModel();
                if (object.has("content"))
                    model.setContent(object.optString("content"));
                if (object.has("chart_id"))
                    model.setChartId(object.optString("chart_id"));
                if (object.has("id"))
                    model.setId(object.optString("id"));
                if (object.has("create_time"))
                    model.setCreateTime(object.optString("create_time"));
                if (object.has("send_user_id"))
                    model.setmSenderId(object.optString("send_user_id"));
                if (object.has("receive_user_id"))
                    model.setmTargetId(object.optString("receive_user_id"));
                if (object.has("send_user_name"))
                    model.setmSenderName(object.optString("send_user_name"));
                if (object.has("send_avatar_url"))
                    model.setmSenderUrl(object.optString("send_avatar_url"));
                if (object.has("own"))
                    model.setmIsOwn(object.optString("own"));
                // 获取对方的信息
                if (object.has("receive_user_name"))
                    model.setListTargetName(object.getString("receive_user_name"));
                models.add(model);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取搜索专题列表
     */
    public List<IModel.ISheetModel> downQTopicListData(JSONObject jsonObject) {
        List<IModel.ISheetModel> models = new ArrayList<>();
        try {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                models.add(parseSheetModel(object));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }

    /**
     * 获取视频源播放器标签名
     */
    @NotNull
    public List<VideoSideTagModel> downVideoTag(@NotNull JSONObject json) {
        List<VideoSideTagModel> models = new ArrayList<>();
        VideoSideTagModel model;
        try {
            JSONArray jsonArray = json.getJSONObject("data").getJSONArray("list");
            for (int i = 0; i < jsonArray.length(); i++) {
                model = new VideoSideTagModel();
                JSONObject object = jsonArray.getJSONObject(i);
                if (object.has("name")) {
                    model.setMSideName("name");
                }
                if (object.has("value")) {
                    model.setMFullVideoTag("value");
                }
                models.add(model);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return models;
    }
}
